package com.sokal.easycare.modules.ad.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.modules.ad.entity.AdInfo;
import com.sokal.easycare.modules.ad.entity.AdInfoFile;
import com.sokal.easycare.modules.ad.service.AdInfoService;
import com.sokal.easycare.modules.picture.entity.PicArticleFile;

/**
 * 广告Controller
 * @author liliangdong
 * @version 2017-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adInfo")
public class AdInfoController extends BaseController {

	@Autowired
	private AdInfoService adInfoService;
	
	@ModelAttribute
	public AdInfo get(@RequestParam(required=false) String id) {
		AdInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = adInfoService.get(id);
		}
		if (entity == null){
			entity = new AdInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("ad:adInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(AdInfo adInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AdInfo> page = adInfoService.findPage(new Page<AdInfo>(request, response), adInfo); 
		model.addAttribute("page", page);
		return "modules/ad/adInfoList";
	}

	@RequiresPermissions("ad:adInfo:view")
	@RequestMapping(value = "form")
	public String form(AdInfo adInfo, Model model) {
		model.addAttribute("adInfo", adInfo);
		return "modules/ad/adInfoForm";
	}

	@RequiresPermissions("ad:adInfo:edit")
	@RequestMapping(value = "save")
	public String save(AdInfo adInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, adInfo)){
			return form(adInfo, model);
		}
		adInfoService.save(adInfo);
		addMessage(redirectAttributes, "保存广告成功");
		return "redirect:"+Global.getAdminPath()+"/ad/adInfo/?repage";
	}
	
	@RequiresPermissions("ad:adInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(AdInfo adInfo, RedirectAttributes redirectAttributes) {
		adInfoService.delete(adInfo);
		addMessage(redirectAttributes, "删除广告成功");
		return "redirect:"+Global.getAdminPath()+"/ad/adInfo/?repage";
	}
	
	/**
	 * 上传文章图片
	 * @param articleFile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "uploadAdInfoFile")
	public String uploadAdInfoFile(@RequestParam(value = "adInfoFile") MultipartFile adInfoFile) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			String fileUrl=FastDFSClient.uploadFile(adInfoFile, adInfoFile.getOriginalFilename());
			AdInfoFile _adInfoFile=new AdInfoFile();
			_adInfoFile.setFileUrl(fileUrl);
			_adInfoFile.setFileName(adInfoFile.getOriginalFilename());
			//picArticleFile.setFastdfs_file_url(Global.getConfig("fastdfs_file_url"));
			map.put("success", true);
			map.put("adInfoFile", _adInfoFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}

}